/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DatabazoveOkno;

import java.awt.Frame;
import java.util.ArrayList;
import javax.swing.JInternalFrame;

/**
 *
 * @author Riso-Desktop
 */
public class Filter extends javax.swing.JDialog {

    /**
     * Creates new form Filter
     */
    DatabazoveOkno datokno;
    ArrayList<String> nazvyStlpcov;
    String sql = null;

    public Filter(java.awt.Frame parent, boolean modal, DatabazoveOkno fr) {
        super(parent, modal);
        initComponents();
        datokno = fr;
        nacitanie(parent);
        doKomboboxu();
    }

    private void nacitanie(Frame parent) {
        this.setLocation(parent.getWidth() / 2 - this.getWidth() / 2, parent.getHeight() / 2 - this.getHeight() / 2);
        nazvyStlpcov = datokno.getNazvyStlpcov();
        jButton3.setVisible(false);
        jCheckBox2.setVisible(false);
        jComboBox2.setVisible(false);
        jTextField2.setVisible(false);
        jLabel3.setVisible(false);
        jLabel4.setVisible(false);
        jButton4.setVisible(false);
        jComboBox3.setVisible(false);
        jTextField3.setVisible(false);
        jLabel6.setVisible(false);
        jLabel5.setVisible(false);
        this.setBounds(this.getX(), this.getY(), 400, 200);
    }

    private void doKomboboxu() {
        jComboBox1.removeAll();
        jComboBox2.removeAll();
        jComboBox3.removeAll();
        for (int i = 0; i < nazvyStlpcov.size(); i++) {
            jComboBox1.addItem(nazvyStlpcov.get(i));
            jComboBox2.addItem(nazvyStlpcov.get(i));
            jComboBox3.addItem(nazvyStlpcov.get(i));
        }
    }

    public String getSQL() {
        return sql;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jComboBox1 = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jButton2 = new javax.swing.JButton();
        jCheckBox1 = new javax.swing.JCheckBox();
        jTextField2 = new javax.swing.JTextField();
        jButton3 = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        jComboBox2 = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        jCheckBox2 = new javax.swing.JCheckBox();
        jButton4 = new javax.swing.JButton();
        jComboBox3 = new javax.swing.JComboBox();
        jTextField3 = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setText("Stĺpec:");

        jButton1.setText("OK");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel2.setText("=");

        jButton2.setText("Vymaz Filter");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jCheckBox1.setText("ďalší filter");
        jCheckBox1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCheckBox1ActionPerformed(evt);
            }
        });

        jButton3.setText("OK");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel3.setText("=");

        jLabel4.setText("Stĺpec:");

        jCheckBox2.setText("ďalší filter");
        jCheckBox2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCheckBox2ActionPerformed(evt);
            }
        });

        jButton4.setText("OK");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        jLabel5.setText("Stĺpec:");

        jLabel6.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel6.setText("=");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 165, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextField1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(jButton2))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jCheckBox1)
                            .addComponent(jLabel1)
                            .addComponent(jLabel4)
                            .addComponent(jCheckBox2)
                            .addComponent(jLabel5))
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, 165, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextField2, javax.swing.GroupLayout.DEFAULT_SIZE, 122, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jComboBox3, javax.swing.GroupLayout.PREFERRED_SIZE, 165, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel6)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextField3, javax.swing.GroupLayout.DEFAULT_SIZE, 122, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jButton2)
                .addGap(15, 15, 15)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton1)
                    .addComponent(jLabel2)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jCheckBox1)
                .addGap(15, 15, 15)
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton3)
                    .addComponent(jLabel3)
                    .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jCheckBox2)
                .addGap(15, 15, 15)
                .addComponent(jLabel5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jComboBox3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton4)
                    .addComponent(jLabel6)
                    .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private String prvam(){
        String s = jTextField1.getText();
        StringBuilder moznosti = new StringBuilder();
        if (s.indexOf(",") != -1) {
            while (s.indexOf(",") != -1) {
                String pom = s.substring(0, s.indexOf(","));
                moznosti.append(jComboBox1.getSelectedItem().toString() + " = '");
                moznosti.append(pom);
                s = s.substring(s.indexOf(",") + 1);
                while (!s.substring(0).equals(s.substring(s.length()))&&s.substring(0, 1).equals(" ")) {
                    s = s.substring(1);
                }
                if (s.indexOf(",") != -1) {
                    moznosti.append("' or ");
                } else {
                    moznosti.append("' or ");
                    moznosti.append(jComboBox1.getSelectedItem().toString() + " = '" + s + "'");
                }
            }
        } else {
            while (s.substring(0, 1).equals(" ")) {
                s = s.substring(1);
            }
            moznosti.append(jComboBox1.getSelectedItem().toString() + " = '" + s + "'");
        }
        return moznosti.toString();
    }
    
    private String druham(){
        String s = jTextField2.getText();
        StringBuilder moznosti = new StringBuilder();
        if (s.indexOf(",") != -1) {
            while (s.indexOf(",") != -1) {
                String pom = s.substring(0, s.indexOf(","));
                moznosti.append(jComboBox2.getSelectedItem().toString() + " = '");
                moznosti.append(pom);
                s = s.substring(s.indexOf(",") + 1);
                while (!s.substring(0).equals(s.substring(s.length()))&&s.substring(0, 1).equals(" ")) {
                    s = s.substring(1);
                }
                if (s.indexOf(",") != -1) {
                    moznosti.append("' or ");
                } else {
                    moznosti.append("' or ");
                    moznosti.append(jComboBox2.getSelectedItem().toString() + " = '" + s + "'");
                }
            }
        } else {
            while (s.substring(0, 1).equals(" ")) {
                s = s.substring(1);
            }
            moznosti.append(jComboBox2.getSelectedItem().toString() + " = '" + s + "'");
        }
        return moznosti.toString();
    }
    
    private String tretiam(){
        String s = jTextField3.getText();
        StringBuilder moznosti = new StringBuilder();
        if (s.indexOf(",") != -1) {
            while (s.indexOf(",") != -1) {
                String pom = s.substring(0, s.indexOf(","));
                moznosti.append(jComboBox3.getSelectedItem().toString() + " = '");
                moznosti.append(pom);
                s = s.substring(s.indexOf(",") + 1);
                while (!s.substring(0).equals(s.substring(s.length()))&&s.substring(0, 1).equals(" ")) {
                    s = s.substring(1);
                }
                if (s.indexOf(",") != -1) {
                    moznosti.append("' or ");
                } else {
                    moznosti.append("' or ");
                    moznosti.append(jComboBox3.getSelectedItem().toString() + " = '" + s + "'");
                }
            }
        } else {
            while (s.substring(0, 1).equals(" ")) {
                s = s.substring(1);
            }
            moznosti.append(jComboBox3.getSelectedItem().toString() + " = '" + s + "'");
        }
        return moznosti.toString();
    }
    
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        
        sql = "select * from " + datokno.meno + " where " + prvam();
        this.setVisible(false);
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        sql = "select * from " + datokno.meno;
        this.setVisible(false);
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        //TODO: pridať možnosť viacnásobného filtrovania
        sql = "select * from " + datokno.meno + " where (" + prvam() +") and " + druham();
        this.setVisible(false);
        System.out.println(sql);
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        //TODO: pridať možnosť viacnásobného filtrovania
        sql = "select * from " + datokno.meno + " where ((" + prvam() +") and " + druham()+ ") and "+ tretiam();
        this.setVisible(false);
    }//GEN-LAST:event_jButton4ActionPerformed

    private void jCheckBox2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCheckBox2ActionPerformed
        if (jCheckBox2.isSelected()) {
            jButton4.setVisible(true);
            jComboBox3.setVisible(true);
            jTextField3.setVisible(true);
            jLabel6.setVisible(true);
            jLabel5.setVisible(true);
            this.setBounds(this.getX(), this.getY(), 400, this.getHeight() + 70);
        } else {
            jButton4.setVisible(false);
            jComboBox3.setVisible(false);
            jTextField3.setVisible(false);
            jLabel6.setVisible(false);
            jLabel5.setVisible(false);
            this.setBounds(this.getX(), this.getY(), 400, this.getHeight() - 70);
        }
    }//GEN-LAST:event_jCheckBox2ActionPerformed

    private void jCheckBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCheckBox1ActionPerformed
        if (jCheckBox1.isSelected()) {
            jButton3.setVisible(true);
            jCheckBox2.setVisible(true);
            jComboBox2.setVisible(true);
            jTextField2.setVisible(true);
            jLabel3.setVisible(true);
            jLabel4.setVisible(true);
            this.setBounds(this.getX(), this.getY(), 400, this.getHeight() + 70);
        } else {
            jButton3.setVisible(false);
            jCheckBox2.setVisible(false);
            jComboBox2.setVisible(false);
            jTextField2.setVisible(false);
            jLabel3.setVisible(false);
            jLabel4.setVisible(false);
            if (jCheckBox2.isSelected()) {
                jButton4.setVisible(false);
                jComboBox3.setVisible(false);
                jTextField3.setVisible(false);
                jLabel6.setVisible(false);
                jLabel5.setVisible(false);
                jCheckBox2.setSelected(false);
                this.setBounds(this.getX(), this.getY(), 400, this.getHeight() - 70);
            }
            this.setBounds(this.getX(), this.getY(), 400, this.getHeight() - 70);
        }
    }//GEN-LAST:event_jCheckBox1ActionPerformed
    /**
     * @param args the command line arguments
     */
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JCheckBox jCheckBox1;
    private javax.swing.JCheckBox jCheckBox2;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JComboBox jComboBox2;
    private javax.swing.JComboBox jComboBox3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    // End of variables declaration//GEN-END:variables
}
